home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c
- Subject: Re: Question on string literals and char arrays
- Date: Fri, 23 Feb 1996 07:32:07 GMT
- Organization: Netcom
- Message-ID: <312d6b0d.113556285@nntp.ix.netcom.com>
- References: <KEITHBAR.96Feb22192211@owl.WPI.EDU>
- NNTP-Posting-Host: ix-dc9-12.ix.netcom.com
- X-NETCOM-Date: Thu Feb 22 11:32:02 PM PST 1996
- X-Newsreader: Forte Agent .99d/32.182
-
- keithbar@owl.WPI.EDU (Keith Barrett) wrote:
-
- >
- > This may have been asked before but here it is:
- >
- > Could someone explain to me in very great detail what is the
- > difference between:
- >
- > main()
- > {
- > char *s;
- > s = "Keith";
- > printf("%s\n", s);
- > }
- >
- > and
- >
- > main()
- > {
- > char s[6];
- > s = "Keith";
- > printf("%s\n", s);
- > }
- >
- > In the compiler I use (gcc version 2.7.2) the second one will not
- > compile and the first does and works. I understand the surface of this
- > problem and I am a grad student in computer science so I am looking
- > for details.
-
- You found the difference -- the former is legal and the latter is not
- :-)
-
- char *s defines s as a variable containing a pointer to char. A value
- may be assigned to it by assigning a new pointer.
-
- char s[6] defines s as an array of char. Arrays may not be assigned
- with the assignment operator. To store a new value you have to move
- it in with something like strcpy(). E.g.,
-
- strcpy(s, "Keith");
-
- Note that you can do this if s is a pointer, but only if it has been
- suitably initialized.
-
- You can initialize an array directly with something like
-
- char s[6] = "Keith";
-
- but you can't assign to s after that.
-
- >
- > I realize the s in the second example acts as a a const char * and
- > theirfore can't be changed. That is why
- >
- > char s[6] = "Keith";
- >
- > is allowed.
-
- No. s does not act as a const char *. It acts as an array.
-
- >
- > My questions are:
- >
- > 1) where exactly is the string literal located. I realize it has
- > global scope so I assume it is created right in the data segment.
-
- The compiler puts the string literal someplace convenient. Just where
- depends on the compiler. It may be put in read only memory, do don't
- try to change the literal.
-
- Note that in
-
- char s[6] = "Keith";
-
- the string literal need not actually be stored. In many cases the
- compiler will simply put the correct valued directly in the array.
-
- >
- > 2) what does it evaluate to. I think is is a static const char * const
- > (a static constant char pointer to a constant char).
- >
- > "Keith"[2] evaluates to a const char right
-
- No. It evaluates to a char. Note, however, that you cannot safely
- change it.
-
- > 3) I know that a pointer variable of type (char *) is created for the
- > first example but what about example two. Is s a (const char *)? Some
- > books say that s in this case is not even created. Because it is a
- > const char pointer and can't change it is never created. The real
- > address is just used everywhere s is rather than looking it up.
- > Assuming a non optimizing compiler.
-
- No. s is an array. In many situations it is converted to a char *.
- It is not a const char pointer.
-
- You seem to be under the common misapprehension that pointers and
- arrays are the same thing -- they are not.
-
- >
- >
- > Any help would be appreciated,
- > Keith Barrett
-
-
- Michael M Rubenstein
-